﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Agenda_Project.Models;

namespace Agenda_Project.Controllers
{
    [Authorize]
    public class CommitteeController : Controller
    {
        private AgendaDBEntities db = new AgendaDBEntities();

        private User GetUser(string username)
        {
            List<User> users = db.Users.Where<User>(u => u.Name.Equals(username)).ToList<User>();

            if (users.Count > 0)
                return users[0];
            else
                return null;
        }

        //
        // GET: /Committee/

        public ActionResult Index()
        {
            var committees = db.Committees.Include(c => c.CancelBy).Include(c => c.CreateBy);
            return View(committees.ToList());
        }

        //
        // GET: /Committee/Details/5

        public ActionResult Details(long id = 0)
        {
            Committee committee = db.Committees.Find(id);
            if (committee == null)
            {
                return HttpNotFound();
            }
            return View(committee);
        }

        //
        // GET: /Committee/Create

        public ActionResult Create()
        {
            ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name");
            ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name");
            return View();
        }

        //
        // POST: /Committee/Create

        [HttpPost]
        public ActionResult Create(Committee committee)
        {
            if (ModelState.IsValid)
            {
                db.Committees.Add(committee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name", committee.CancelByID);
            ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name", committee.CreateByID);
            return View(committee);
        }

        //
        // GET: /Committee/Edit/5

        public ActionResult Edit(long id = 0)
        {
            Committee committee = db.Committees.Find(id);
            if (committee == null)
            {
                return HttpNotFound();
            }
            ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name", committee.CancelByID);
            ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name", committee.CreateByID);
            return View(committee);
        }

        //
        // POST: /Committee/Edit/5

        [HttpPost]
        public ActionResult Edit(Committee committee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(committee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name", committee.CancelByID);
            ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name", committee.CreateByID);
            return View(committee);
        }

        //
        // GET: /Committee/Delete/5

        public ActionResult Delete(long id = 0)
        {
            Committee committee = db.Committees.Find(id);
            if (committee == null)
            {
                return HttpNotFound();
            }
            return View(committee);
        }

        //
        // POST: /Committee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Committee committee = db.Committees.Find(id);
            db.Committees.Remove(committee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /CommitteeMember/Details/5

        public ActionResult MemberDetails(long id = 0)
        {
            CommitteeMember committeemember = db.CommitteeMembers.Find(id);
            if (committeemember == null)
            {
                return HttpNotFound();
            }
            return View(committeemember);
        }

        //
        // GET: /CommitteeMember/Create

        public ActionResult MemberCreate(int id = 0)
        {
            CommitteeMember member = new CommitteeMember();
            ViewBag.CommitteeRoleID = new SelectList(db.CommitteeRoles, "ID", "Name");
            //ViewBag.CommitteeID = new SelectList(db.Committees, "ID", "Name");
            //ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name");
            //ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name");
            ViewBag.UserID = new SelectList(db.Users, "ID", "Name");

            member.CommitteeID = id;

            return View(member);
        }

        //
        // POST: /CommitteeMember/Create

        [HttpPost]
        public ActionResult MemberCreate(CommitteeMember committeemember)
        {
            if (ModelState.IsValid)
            {
                committeemember.Active = true;
                committeemember.CreateByID = this.GetUser(System.Web.HttpContext.Current.User.Identity.Name).ID;
                db.CommitteeMembers.Add(committeemember);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CommitteeRoleID = new SelectList(db.CommitteeRoles, "ID", "Name", committeemember.CommitteeRoleID);
            //ViewBag.CommitteeID = new SelectList(db.Committees, "ID", "Name", committeemember.CommitteeID);
            //ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name", committeemember.CancelByID);
            //ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name", committeemember.CreateByID);
            ViewBag.UserID = new SelectList(db.Users, "ID", "Name", committeemember.UserID);
            return View(committeemember);
        }

        //
        // GET: /CommitteeMember/Edit/5

        public ActionResult MemberEdit(long id = 0)
        {
            CommitteeMember committeemember = db.CommitteeMembers.Find(id);
            if (committeemember == null)
            {
                return HttpNotFound();
            }
            ViewBag.CommitteeRoleID = new SelectList(db.CommitteeRoles, "ID", "Name", committeemember.CommitteeRoleID);
            //ViewBag.CommitteeID = new SelectList(db.Committees, "ID", "Name", committeemember.CommitteeID);
            //ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name", committeemember.CancelByID);
            //ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name", committeemember.CreateByID);
            ViewBag.UserID = new SelectList(db.Users, "ID", "Name", committeemember.UserID);
            return View(committeemember);
        }

        //
        // POST: /CommitteeMember/Edit/5

        [HttpPost]
        public ActionResult MemberEdit(CommitteeMember committeemember)
        {
            if (ModelState.IsValid)
            {
                db.Entry(committeemember).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CommitteeRoleID = new SelectList(db.CommitteeRoles, "ID", "Name", committeemember.CommitteeRoleID);
            //ViewBag.CommitteeID = new SelectList(db.Committees, "ID", "Name", committeemember.CommitteeID);
            //ViewBag.CancelByID = new SelectList(db.Users, "ID", "Name", committeemember.CancelByID);
            //ViewBag.CreateByID = new SelectList(db.Users, "ID", "Name", committeemember.CreateByID);
            ViewBag.UserID = new SelectList(db.Users, "ID", "Name", committeemember.UserID);
            return View(committeemember);
        }

        //
        // GET: /CommitteeMember/Delete/5

        public ActionResult MemberDelete(long id = 0)
        {
            CommitteeMember committeemember = db.CommitteeMembers.Find(id);
            if (committeemember == null)
            {
                return HttpNotFound();
            }
            return View(committeemember);
        }

        //
        // POST: /CommitteeMember/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult MemberDeleteConfirmed(long id)
        {
            CommitteeMember committeemember = db.CommitteeMembers.Find(id);
            db.CommitteeMembers.Remove(committeemember);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}